<?php
	/**
	 *	KLF Framework
	 *
	 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 22, 2011
	 *	Copyright (C) 2010-2011
	 */
	namespace KLF\System\Test
	{
		/**
		 *  Class KUnitTestCase
		 *
		 *  The base unit testcase
		 *
		 *  @package KLF\System\Test
		 *  @access public
		 *  @author Kristian Oye <kristianoye@gmail.com>
		 *  @since 0.1
		 */
		abstract class KUnitTestCase
		{
			protected $_Failures;
			protected $_Result;
			protected $_Skipped;
			protected $_Successes;
			protected $_TestCount;
			protected $_TestName;
			
			
			public function __construct($name = '')
			{
				$this->_TestName = $name;
				$this->_Failures = 
					$this->_Successes = 
					$this->_TestCount = 
					$this->_Skipped = 0;
				$this->_Result = new KUnitTestResult($this->_TestName);
			}
			
			/**
			 * Contains setup logic for the testcase.
			 */
			public function Register()
			{
			}
			
			/**
			 * Returns true if the test was successful.
			 */
			public function Successful()
			{
			}
			
			public function RunTest()
			{
				$testMethodList = array_filter(
					get_class_methods(get_called_class()), 
					function($methodName)
					{
						return (substr($methodName, 0, 4) === 'test');
					});
				
				foreach($testMethodList as $method)
				{
					$result = $this->_Result->GetMethodResult($method);
					try
					{
						$this->_TestCount++;

						$result->TimeStart();
						$this->$method();
						$result->TimeEnd();
						
						$this->_Successes++;
					}
					catch(\Exception $ex)
					{
						$result->TimeEnd();
						$this->_Failures++;
					}
				}
			}
			
			/**
			 * Contains cleanup logic for the testcase.
			 */
			public function UnRegister()
			{
			}
			
			public function __toString()
			{
				return sprintf("%s (%s) [%d PASS/%d FAILED of %d TOTAL [%d SKIPPED]",
					$this->_TestName,
					get_class($this),
					$this->_Successes,
					$this->_Failures,
					$this->_TestCount,
					$this->_Skipped);
			}
		}
	}